var express = require('express');
const multer = require('multer')
const Result = require('../lib/Result');
const isRule = require('../util/isRule');
const Image = require('../db/models/image/image');
var router = express.Router();
const upload = multer({ dest: '../assets' })//图片上传中间件
/**
 * 图片上传,token需要67号权限，或11号权限，或172号权限,或super==1
 * 只接受单个图片,类型设置为multipart/form-data
 */
router.post("/upload", upload.single("img"), (req, res) => {
    let token = req.auth
    if (isRule(token.rule, [67, 11, 172], res, token.super)) {
        console.log(req.file);
        Image.addImage(req.body.image_class_id,req.file.filename).then(value=>{
            res.json(Result.success(value.toJSON()))
        })
    }
})
/**
 * 批量删除图片,token需要68号权限，或11号权限，或172号权限,或super==1
 */
router.post("/delete_all", (req, res) => {
    let token = req.auth
    if (isRule(token.rule, [68, 11, 172], res, token.super)) {
        Image.deleteImage(req.body.ids).then(res.json(Result.success(true)))
    }
})
/**
 * 修改图片名称,token需要69号权限，或11号权限，或172号权限,或super==1
 * 注意这个只修改了数据库里面的名称，并不会改变真正的用户名
 */
router.post("/:id", (req, res) => {
    let token = req.auth
    if (isRule(token.rule, [69, 11, 172], res, token.super)) {
        Image.updateImage(req.params.id,req.body.name)
    }
})
module.exports = router